package com.orangesale.cn.fragment;

import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.Spinner;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.orangesale.cn.R;
import com.orangesale.cn.adapter.ListViewAdapter;
import com.orangesale.cn.adapter.ProductAdapter;
import com.orangesale.cn.entity.Condition;
import com.orangesale.cn.entity.OrangeProductPack;
import com.orangesale.cn.netrequest.OkHttpClientProduct;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ProductFragment extends Fragment {
    private Spinner conditonListSpinner;
    private ListViewAdapter listViewAdapter;
    private List<Condition> conditionList;
    private GridView productGridView;
    private List<OrangeProductPack> orangeProductList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.content_product, container, false);
        init(view);
        return view;
    }

    /**
     * 组件初始化方法
     *
     * @param view
     */
    private void init(View view) {
        conditonListSpinner = view.findViewById(R.id.product_select_condition);
        initCondList();
        listViewAdapter = new ListViewAdapter(getActivity(), conditionList);
        conditonListSpinner.setAdapter(listViewAdapter);
        productGridView = view.findViewById(R.id.product_list);
        new SearchProductTask().execute();
    }

    /**
     * 初始化conditionList
     */
    private void initCondList() {
        conditionList = new ArrayList<>();
        Condition allCondition = new Condition();
        allCondition.setConditionIcon(R.drawable.all);
        allCondition.setConditionName("全部");
        Condition saleCondition = new Condition();
        saleCondition.setConditionIcon(R.drawable.salenum);
        saleCondition.setConditionName("按销量高低排序");
        Condition timeCondition = new Condition();
        timeCondition.setConditionIcon(R.drawable.time);
        timeCondition.setConditionName("按上市时间排序");
        Condition priceCondition = new Condition();
        priceCondition.setConditionIcon(R.drawable.price);
        priceCondition.setConditionName("按商品价格排序");
        conditionList.add(allCondition);
        conditionList.add(saleCondition);
        conditionList.add(timeCondition);
        conditionList.add(priceCondition);
    }

    /**
     * 发送网络请求获取数据
     */
    class SearchProductTask extends AsyncTask<Void, Void, List<OrangeProductPack>> {

        @Override
        protected List<OrangeProductPack> doInBackground(Void... voids) {
            OkHttpClientProduct clientProduct = new OkHttpClientProduct();
            try {
                orangeProductList = clientProduct.getProductPack();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return orangeProductList;
        }

        @Override
        protected void onPostExecute(List<OrangeProductPack> orangeProducts) {
            ProductAdapter productAdapter = new ProductAdapter(getActivity(), orangeProductList);
            productGridView.setAdapter(productAdapter);
        }
    }


}
